package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.vo.UserPersonalInfoVo;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.entities.Employeeemails;
import com.opgea.crm.domain.entities.Employeephones;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.modal.QualifierModel;
import com.opgea.crm.repository.EmployeeRepo;

@Service
public class EmployeeServiceImpl implements EmployeeService{

	@Autowired
	EmployeeRepo employeeRepo;

	
	public void create(Employee employee) {
		employeeRepo.create(employee);
	}
	
	
	public void update(Employee employee) {
		employeeRepo.update(employee);
	}	

	public void remove(long id) {
		employeeRepo.remove(id);
	}

	public Employee find(long id) {
		return employeeRepo.find(id);
	}

	public List<UserPersonalInfoVo> findAll() {
		List<UserPersonalInfoVo> userList = new ArrayList<UserPersonalInfoVo>();
		List<Employee> empList = employeeRepo.findAll();
		
		for(Employee  emp : empList){
			
			UserPersonalInfoVo userInfoVo = new UserPersonalInfoVo();
			userInfoVo.setEmployeeId(emp.getId());
			userInfoVo.setFirstName(emp.getFirstName());
			userInfoVo.setLastName(emp.getLastName());
			userInfoVo.setMiddleInitial(emp.getMiddleInitial());
			userInfoVo.setTitle(emp.getTitle());
			userInfoVo.setEmpCode(emp.getEmpCode());
			
			if(emp.getEmployeeemailsCollection().size()>0){
				userInfoVo.setEmail(new ArrayList<Employeeemails>(emp.getEmployeeemailsCollection()).get(0).getAddress());
			}
			if(emp.getEmployeephonesCollection().size()>0){
				userInfoVo.setPhone1(new ArrayList<Employeephones>(emp.getEmployeephonesCollection()).get(0).getPhoneNo());
			}
			if(emp.getEmployeephonesCollection().size()>1){
				userInfoVo.setPhone2(new ArrayList<Employeephones>(emp.getEmployeephonesCollection()).get(1).getPhoneNo());
			}
			userList.add(userInfoVo);
		}
		return userList;		
	}
	
	

	public List<UserPersonalInfoVo> findUserList(String criteria, String value) {
		List<UserPersonalInfoVo> userList = new ArrayList<UserPersonalInfoVo>();
		List<Employee> empList = employeeRepo.findUserList(criteria, value);
		
		for(Employee emp: empList){
			UserPersonalInfoVo userInfoVo = new UserPersonalInfoVo();
			userInfoVo.setEmployeeId(emp.getId());
			userInfoVo.setFirstName(emp.getFirstName());
			userInfoVo.setLastName(emp.getLastName());
			userInfoVo.setMiddleInitial(emp.getMiddleInitial());
			userInfoVo.setTitle(emp.getTitle());
			userInfoVo.setEmpCode(emp.getEmpCode());
			
			if(emp.getEmployeeemailsCollection().size()>0){
				userInfoVo.setEmail(new ArrayList<Employeeemails>(emp.getEmployeeemailsCollection()).get(0).getAddress());
			}
			if(emp.getEmployeephonesCollection().size()>0){
				userInfoVo.setPhone1(new ArrayList<Employeephones>(emp.getEmployeephonesCollection()).get(0).getPhoneNo());
			}
			if(emp.getEmployeephonesCollection().size()>1){
				userInfoVo.setPhone2(new ArrayList<Employeephones>(emp.getEmployeephonesCollection()).get(1).getPhoneNo());
			}
			userList.add(userInfoVo);
		}
		
		return userList;
	}


	public List<SetupsValue> findAllEmployeeName() {
		List<Employee> employees = employeeRepo.findAll();
		List<SetupsValue> employeeNameList = new ArrayList<SetupsValue>();
		for(Employee employee : employees){
			employeeNameList.add(new SetupsValue(employee.getId(), employee.getFirstName()));
		}
		return employeeNameList;
	}
	


	public List<QualifierModel> findAllEmployeeNameByCompanyId(long companyId) {
		List<Employee> employees = employeeRepo.findByCompanyId(companyId);
		List<QualifierModel> employeeNameList = new ArrayList<QualifierModel>();
		System.out.println("Inside service impl");
		for(Employee employee : employees)
		{
			employeeNameList.add(new QualifierModel(employee.getId(), employee.getFirstName()));
			System.out.println("Assign To : id : "+employee.getId()+", Value :  "+employee.getFirstName());
		}
		return employeeNameList;
	}
	
	public List<QualifierModel> getUserSearchCriteriaTypes(){
		List<QualifierModel> userSearchTypes = new ArrayList<QualifierModel>();
		for(ApplicationConstant.UserSearchCriteriaQualifier qualifier : ApplicationConstant.UserSearchCriteriaQualifier.values()){
			userSearchTypes.add(new QualifierModel(Long.valueOf(qualifier.ordinal()), qualifier.name()));
			System.out.println("User Search Type : "+qualifier);
		}
		return userSearchTypes;
	}



	public List<SetupsValue> findallEmployeeNameByLeadId(){
		List<Employee>  employees = employeeRepo.findByLeadId();
		List<SetupsValue> employeeNameList = new ArrayList<SetupsValue>();
		for(Employee employee : employees)
		{
			employeeNameList.add(new SetupsValue(employee.getId(), employee.getFirstName()));
		}
		return employeeNameList;
	}


	
	
}
